angular.module('goodDetailModule', ['ionic'])
.controller("goodDetailCtrl",["$scope","$http","getDetail","$state",function($s,$h,gd,$state){
	gd.getdetails()
	.then(function(res){
//		console.log(res.data.data)
		$s.data = res.data.data
		$s.imgs = res.data.data.photoAlbum;
		$s.persent = res.data.data.commentStatistics.goodCount/res.data.data.commentStatistics.commentCount*100
		function transform(obj){
		    var arr = [];
		    for(var item in obj){
		        arr.push(obj[item]);
		    }
		    return arr;
		}
		$s.len = transform($s.imgs).length
	});
	gd.jt()
	.then(function(data){
		//console.log(data.data.data)
		$s.teletext = data.data.data.teletext
		$s.sizeMaterial = data.data.data.sizeMaterial
		$s.commentStatistics = data.data.data.commentStatistics
	});
	$s.isLoadingMore = true;
	$s.clickBtn = function(j){
		$s.boolphototext = false
		$s.boolsize = false
		$s.boolvalue = true
		gd.comments(j)
		.then(function(com){
			console.log(com.data.data.list)
			$s.lists = com.data.data.list
		});
//		$s.loadMoreh = function(j){
//			
//			if($s.isLoadingMore && !$s.lists){
//			$s.isLoadingMore = false;
//			
//			
//			gd.commentss(j)
//			.then(function(com){
//				console.log(com.data.data.list)
//				$s.isLoadingMore = true;
//				$s.lists = com.data.data.list;
//				$s.$broadcast('scroll.infiniteScrollComplete');			
//				console.log(com.data.data.list)
//				console.log("开始加载更多了")
//			});
//		}
//			if(!$s.lists)
//				$s.$broadcast('scroll.infiniteScrollComplete');			
//			
//			}
	}
	//点击切换
	$s.boolphototext = true
	$s.boolsize = false
	$s.boolvalue = false
	$s.phototext = function(){
		$s.boolphototext = true
		$s.boolsize = false
		$s.boolvalue = false
	}
	$s.size = function(){
		$s.boolphototext = false
		$s.boolsize = true
		$s.boolvalue = false
	}
//	$s.value = function(){
//		$s.boolphototext = false
//		$s.boolsize = false
//		$s.boolvalue = true
//	}
	
}])
.filter("myfilte",function(){
	return function(value){
		if(value){
			return value.toFixed(2)
		}
	}
})
.factory("getDetail",function($http,$state){
//	console.log($state)
	var getdetails = function(){
		return $http.get("/icy/m.php?method=goods.getInfo&isFromApp=1",{
			params:{
				goodsId:$state.params.detail,
			}
		})
		.then(function(res){
			return res
		})
	};
	var jt = function(){
		return $http.get("/icy/m.php?method=goods.getExtra",{
			params:{
				goodsId:$state.params.detail,
			}
		})
		.then(function(data){
			return data
		})
	};
	//评论
	var page = 1
	var comments = function(j){
		return $http.get("/icy/m.php?fromPageId=0&pageSize=40&method=goods.getCommentList&page="+page,{
			params:{
				voteType:j,
				goodsId:$state.params.detail,
			}
		})
		.then(function(com){
			return com
		})
	}
	var commentss = function(j){
		page += 1
		return $http.get("/icy/m.php?fromPageId=0&pageSize=40&method=goods.getCommentList",{
			params:{
				page:page,
				voteType:j,
				goodsId:$state.params.detail,
			}
		})
		.then(function(com){
			return com
		})
	}
	return {
		getdetails:getdetails,
		jt:jt,
		comments:comments,
		commentss:commentss
	}
})
